program WtVod;

uses
//  Forms,
  SysUtils,
  ADODB,
  DB,
  Windows,
  ShellAPI,
  ActiveX,
  ConstUnit in '..\..\ConstUnit.pas';

{$R *.res}

var
  Database: TADOConnection;
  Query: TADOQuery;
  ComputerName: string;


function GetLocalComputerName: string;
var
  pName: PChar;
  dLength: DWORD;
begin
  pName := AllocMem( MAX_COMPUTERNAME_LENGTH );
  try
    dLength := MAX_COMPUTERNAME_LENGTH;
    GetComputerName( pName, dLength);
    Result := pName;
  finally
    FreeMem( pName );
  end;

end;

begin
//  Application.Initialize;
//  Application.Run;
  CoInitialize(nil);
  Database := TADOConnection.Create(nil);
  try
    Database.ConnectionString := Format(DBFormat, [ExtractFilePath(ParamStr(0)) + 'Data\BarData.mdb']);
    Database.Open;
    Query := TADOQuery.Create(nil);
    try
      Query.Connection := Database;
      ComputerName := GetLocalComputerName;
      Query.Sql.Text := Format(' SELECT g.TcpFileHost FROM Groups as g Left JOIN ' +
                               ' ComputerGroup AS cg ON cg.GroupId = g.GroupId WHERE cg.ComputerName = "%s"', [ComputerName]);
      Query.Open;
      if Query.IsEmpty or (Query.Fields[0].AsString = '') then
        Exit;

      ShellExecute(0, 'open', PChar(Query.Fields[0].AsString), nil, nil, SW_SHOW);
    finally
      Query.Free;
    end;    
  finally
    Database.Free;
  end;
  CoUninitialize;
end.
